package com.dataease.indicatorqa.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class ChartDataCacheService {

    private static final String CHART_DATA_KEY_PREFIX = "chart_data:";
    private static final long CACHE_EXPIRATION_HOURS = 24; // 缓存24小时后过期

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 存储图表数据到Redis
     * @param data 图表数据
     * @return 缓存ID
     */
    public String storeChartData(Object data) {
        String id = UUID.randomUUID().toString();
        String key = CHART_DATA_KEY_PREFIX + id;
        
        redisTemplate.opsForValue().set(key, data, CACHE_EXPIRATION_HOURS, TimeUnit.HOURS);
        
        return id;
    }

    /**
     * 从Redis获取图表数据
     * @param id 缓存ID
     * @return 图表数据
     */
    public Object getChartData(String id) {
        String key = CHART_DATA_KEY_PREFIX + id;
        return redisTemplate.opsForValue().get(key);
    }
}